package xq.gwt.ui.crud;

import xq.gwt.mvc.model.BooleanPropertyModel;
import xq.gwt.mvc.model.CommandModel;
import xq.gwt.mvc.model.PersistableEntityModel;
import xq.gwt.ui.crud.localization.CrudDictionary;
import xq.gwt.ui.rpc.AsyncCallbackAdapter;
import xq.gwt.ui.widgets.feedback.FeedbackService;

public class PersistEntityCommand implements CommandModel {
	private PersistableEntityModel entity;
	private EntityEditorModel editorModel;
	
	private static final long serialVersionUID = 5726591479695271712L;

	
	public PersistEntityCommand(PersistableEntityModel entity, EntityEditorModel editorModel){
		this.entity = entity;
		this.editorModel = editorModel;
				
	}
	
	@Override
	public void execute() throws Exception {
		CrudServiceProvider.persistanceService.persistEntity(entity, new PersistEntityCallback());
		FeedbackService.getFeedbackPresenter().showFeedback(CrudDictionary.Constants().saving());
		
	}
	
    private class PersistEntityCallback extends AsyncCallbackAdapter<PersistableEntityModel>{
    	@Override
		public void onSuccess(PersistableEntityModel result) {			
			FeedbackService.getFeedbackPresenter().showFeedback(CrudDictionary.Constants().recordSaved());						
			editorModel.setEntity(result);
		}		
    }

	@Override
	public String getCaption() {		
		return null;
	}

	@Override
	public String getName() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public BooleanPropertyModel isEnabled() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String getFeedback() {
		// TODO Auto-generated method stub
		return null;
	}

}
